<template>
    <div id="record">
        <van-nav-bar title="消费记录" left-text="返回" left-arrow @click-left="onClickLeft" />
        <van-pull-refresh  v-model="refreshing" @refresh="onRefresh">
            <van-list offset="10" v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
                <van-cell v-for="item in record" :key="item.id" center :title="item.content" :value="item.total" />
            </van-list>
        </van-pull-refresh>
    </div>
</template>

<script setup>
import { getCurrentInstance, onBeforeMount } from "vue";

const onClickLeft = () => history.back();

onBeforeMount(() => {
})

// 请求数据
const { proxy } = getCurrentInstance();
let userInfo = proxy.$cookies.get("business") ? proxy.$cookies.get("business") : {};
let record = ref([])

const loading = ref(false);
const finished = ref(false);
const refreshing = ref(false);
let page = 1;
let limit = 10;

const onLoad = async () => {
    if (refreshing.value) {
        refreshing.value = false;
    }
    let data = {
        busid: userInfo.id,
        page,
        limit
    }
    let result = await proxy.$POST({
        url: "/business/record",
        params: data
    })
    loading.value = false;
    if (result.code) {
        page++
        record.value = record.value.concat(result.data)
        return false
    } else {
        finished.value = true;
        return false
    }

};

const onRefresh = () => {
    // 清空列表数据
    finished.value = false;
    record.value = []
    page = 1
    // 重新加载数据
    // 将 loading 设置为 true，表示处于加载状态
    loading.value = true;
    onLoad();
};
</script>